import { WAIT_FOR_EL_TIMEOUT } from "../common/consts"
import { addNetworkEuqlStepToMap } from "../common/listenPageRequest"
import { resolveSelector } from "../common/resolveSelector"

export const EqualToBaseEnvTypeExecutor = {
    text: async ({ planId, suiteId, page, data, opts }) => {
        const selector = resolveSelector(data)
        const { timeout = WAIT_FOR_EL_TIMEOUT } = opts

        await page.waitFor(selector, { timeout })

        const getElementTextContent = async () => page.$eval(selector, element => element.textContent.trim())
        const result = await getElementTextContent()

        return result
    },
    image: async ({ planId, suiteId, page, data, opts }) => {
        const selector = resolveSelector(data)
        const { timeout = WAIT_FOR_EL_TIMEOUT } = opts

        await page.waitFor(selector, { timeout })

        const elementHandler = await page.$(selector)
        const getElementImage = async () => elementHandler.screenshot({ encoding: 'base64', type: 'png' })

        const result = await getElementImage()

        return result
    },
    network: async ({ planId, suiteId, page, data, opts }) => {
        let result = await new Promise((resolve) => {
            addNetworkEuqlStepToMap({ planId, suiteId, data, opts, resolve })
        })

        return result
    },
}